// 路由选择的权限
import type { RouteRecordRaw } from "vue-router";
import router from "../router/index";

// 添加定义路由id属性
type IRoute = RouteRecordRaw & { id?: string };

// 定义全部路由：子一级路由
const allRoutes: IRoute[] = [
    // 重定向到主页
    {
        id: "1",
        name: "/",
        path: "",
        redirect: "/HomeIndex/HomePage"
    },
    // 1.首页
    {
        id: "1",
        name: "HomePage",
        path: "HomePage",
        component: () => import("../views/Home/HomePage.vue")
    },
    // 2.商品管理-商品列表,分类搜索
    {
        id: "2-1",
        name: "GoodsList",
        path: "GoodsList",
        component: () => import("../views/Home/Goods/List.vue")
    },
    {
        id: "2-2",
        name: "GoodsCategory",
        path: "GoodsCategory",
        component: () => import("../views/Home/Goods/Category.vue")
    },
    // 3.轮播图管理-轮播图列表,添加轮播图
    {
        id: "3-1",
        name: "BanList",
        path: "BanList",
        component: () => import("../views/Home/Banner/List.vue")
    },
    {
        id: "3-2",
        name: "BanAdd",
        path: "BanAdd",
        component: () => import("../views/Home/Banner/Add.vue")
    },
    // 4.操作人员管理-操作人员列表,添加操作人员
    {
        id: "4-1",
        name: "AdminList",
        path: "AdminList",
        component: () => import("../views/Home/Admin/List.vue")
    },
    {
        id: "4-2",
        name: "AdminAdd",
        path: "AdminAdd",
        component: () => import("../views/Home/Admin/Add.vue")
    },
    // 5.购物车管理-购物车列表
    {
        id: "5-1",
        name: "ShoppingList",
        path: "ShoppingList",
        component: () => import("../views/Home/ShoppingCar/ShoppingList.vue")
    },
    // 6.用户信息-用户列表
    {
        id: "6-1",
        name: "UserList",
        path: "UserList",
        component: () => import("../views/Home/User/UserList.vue")
    },
    // 7.订单信息-订单列表
    {
        id: "7-1",
        name: "OrderList",
        path: "OrderList",
        component: () => import("../views/Home/Order/OrderList.vue")
    },
    // 8.数据图表-数据图表,简单图表数据
    {
        id: "8-1",
        name: "ChartKLine",
        path: "ChartKLine",
        component: () => import("../views/Home/Chart/KLine.vue")
    },
    {
        id: "8-2",
        name: "ChartData",
        path: "ChartData",
        component: () => import("../views/Home/Chart/Chartdata.vue")
    }
]

export default function getRoutes() {
    console.log("getRoutes()");

    // 1、获取当前用户的权限
    let checkedKeys: string[] = [];
    let currentKeys = sessionStorage.getItem("grants")

    if (currentKeys) {
        checkedKeys = JSON.parse(currentKeys)
    }

    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '1-1', '2', '2-1', '2-2', '3', '3-1', '3-1-R', '3-1-U', '3-1-D', '3-2', '4', '4-1', '4-1-R', '4-1-U', '4-1-D', '4-2', '5', '5-1', '6', '6-1', '7', '7-1', '8', '8-1', '8-2']
    }

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            let currentRoute = { ...item }

            // crud - 对应四位数
            // 先定义一个数组
            let arr = ["0", "0", "0", "0"]
            // 判断item.id中是否包含-C/-R/-U/-D
            if (checkedKeys.includes(item.id + "-C")) {
                arr[0] = "1";
            }
            if (checkedKeys.includes(item.id + "-R")) {
                arr[1] = "1";
            }
            if (checkedKeys.includes(item.id + "-U")) {
                arr[2] = "1";
            }
            if (checkedKeys.includes(item.id + "-D")) {
                arr[3] = "1";
            }

            currentRoute.meta = {
                crud: arr.join("")
            }

            router.addRoute("HomeIndex", currentRoute);
        }
    })


    console.log("router.getRoutes()", router.getRoutes());
}


export const clearCurrentRoutes = () => {
    // 1.获取当前用户的权限
    let checkedKeys: string[] = []

    // 定义获取当前的权限值
    let currentKeys = sessionStorage.getItem("grants")

    if (currentKeys) {
        checkedKeys = JSON.parse(currentKeys)
    }

    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '1-1', '2', '2-1', '2-2', '3', '3-1', '3-2', '4', '4-1', '4-2', '5', '5-1', '6', '6-1', '7', '6-1', '8', '8-1', '8-2']
    }

    // 2.删除当前用户的路由配置

    // 遍历定义的所以子一级路由
    allRoutes.forEach(item => {
        // 如果获取的权限id包含在内
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置:vueRouter对象.addRoute(父级路由的name，路由配置)
            //    router.addRoute("HomeIndex",{...item})
            router.removeRoute(item.name as string)
        }
    })
}